#!/bin/bash

alias echo='echo -e'
srcquota="/${QUOTA_TEST}/srcQuotaDir"
srcdir="${FS_MOUNTPOINT}${srcquota}"
destquota="/${QUOTA_TEST}/destQuotaDir"
destdir="${FS_MOUNTPOINT}${destquota}"
noquotadir="${FS_MOUNTPOINT}/${QUOTA_TEST}/dir_noquota"
LOGFILE="/tmp/quotalog"
src_file="$srcdir/src_file"
dest_file="$destdir/dest_file"
noquota_file="$noquotadir/noquota_file"
global_used=0

msg0="测试所有用例"
msg1="目标文件存在, 源目录和目标都设置配额, 移动源文件覆盖目标文件 (没有超出配额)"
msg2="目标文件存在, 源目录和目标都设置配额, 移动源文件覆盖目标文件 (超出配额)"
msg3="目标文件存在, 源目录<设置>配额，目标目录<没有>配额 (不超出配额)"
msg4="目标文件存在, 源目录<没有>配额，目标目录<设置>配额 (不超出配额)"
msg5="目标文件存在, 源目录<没有>配额，目标目录<设置>配额 (超出配额)"
maxmsg=5

function print_help() {
    name="$0"
    echo "Usage:"
    for (( num=0; num<=${maxmsg}; num++ ));do
        eval echo "'    'sh test_one.sh ${name} ${num} '  ' \${msg${num}}"
    done
    exit
}

function reset_all() {
    clean_quota; #sleep ${WAIT_CLIENT_SYNC};
    echo "=====删除然后重新创建目录 ${srcdir} 和 ${destdir}====="
    func_rmdir ${srcdir}; orcafs-ctl --createdir --nodes=1001 ${srcdir}
    func_rmdir ${destdir}; orcafs-ctl --createdir --nodes=2001 ${destdir}

     # 测试是否两个目录是否在不同 meta 节点
    srcMeta=`orcafs-ctl --getentryinfo ${srcdir} | grep Metadata | awk '{print $3}'`
    descMeta=`orcafs-ctl --getentryinfo ${destdir} | grep Metadata | awk '{print $3}'`
    echo "${srcdir} : ${srcMeta}"
    echo "${destdir} : ${descMeta}"
    if [ ${srcMeta} == ${descMeta} ]; then echo "${srcquota} 和 ${destquota} 在同一个 ${srcMeta}"; exit 1; fi

    echo -n "=====创建目录配额 ${srcquota}(硬阈值$1MB) 和 ${destquota}(硬阈值$2MB)====="
    orcafs-ctl --setquota --dir=${srcdir} --hardlimit=$1M &> /dev/null # 10
    orcafs-ctl --setquota --dir=${destdir} --hardlimit=$2M &> /dev/null # 5
    sleep ${WAIT_CLIENT_SYNC};
    quota_num=`orcafs-ctl --listquota | grep -w DIR | wc -l`
    if ((${quota_num} == 2));then echo "pass"; else echo "not pass"; exit 1; fi; echo "";
}

function clean_all_dir() {
    func_cleandir ${srcdir}
    func_cleandir ${destdir}
    func_cleandir ${noquotadir}
}

function func_get_rename_quota_used() {
    quota_name=$1
    used=`orcafs-ctl --listquota | grep -E -w ${quota_name} | grep -w DIR | awk '{print $8}'`
    #echo -n -e "quota used (${used}):\n  [ls ${WRITEDIR}]: "; ls -l ${WRITEDIR}
    echo "\t${quota_name} quota used (${used})"
    global_used=$used
}

function rename_from_srcquota_to_destquota() {
    src_size=$(($1 * 1024 * 1024))
    dest_size=$(($2 * 1024 * 1024))
    src_want=0
    dest_want=$src_size

    clean_all_dir;
    cmd="dd if=/dev/zero of=$src_file  bs=$src_size count=1";echo "\tExec: ${cmd}"; eval ${cmd} &> /dev/null
    cmd="dd if=/dev/zero of=$dest_file bs=$dest_size count=1";echo "\tExec: ${cmd}"; eval ${cmd} &> /dev/null

    func_get_rename_quota_used ${srcquota}; src_old_used=${global_used}
    func_get_rename_quota_used ${destquota}; dest_old_used=${global_used}

    cmd="mv $src_file $dest_file"; echo "\tExec: ${cmd}"; eval ${cmd} &> ${LOGFILE};
    exceed=`cat ${LOGFILE} | grep "Disk quota exceeded" | wc -l`
    if (( ${exceed} == 1 )); then echo "\tDisk quota exceeded, 测试成功"; fi
    
    echo ""; echo "  after mv:"
    cmd="ls -l $destdir | grep -v total"; echo -n "\t${cmd}\t>>"; eval ${cmd}
    func_get_rename_quota_used ${srcquota}; src_new_used=${global_used}
    func_get_rename_quota_used ${destquota}; dest_new_used=${global_used}

    eval echo -n "==${test_num}==\${msg$test_num}===="
    if (( ${exceed} != 1 )); then
        if (($src_old_used == $src_size)) && (($dest_old_used == $dest_size)) \
           && (($src_new_used == 0)) && (($dest_new_used == $src_old_used))
            then echo "success\n"; else echo "fail\n"; exit 1; fi
    else
        if (($src_old_used == $src_size)) && (($dest_old_used == $dest_size)) \
           && (($src_new_used == $src_size)) && (($dest_new_used == $dest_size))
            then echo "success\n"; else echo "fail\n"; exit 1; fi
    fi
}

function rename_from_srcquota_to_noquota() {
    src_size=$(($1 * 1024 * 1024))
    noquota_size=$(($2 * 1024 * 1024))
    src_want=0
    dest_want=$src_size

    clean_all_dir;
    cmd="dd if=/dev/zero of=$src_file  bs=$src_size count=1";echo "\tExec: ${cmd}"; eval ${cmd} &> /dev/null
    cmd="dd if=/dev/zero of=$noquota_file bs=$noquota_size count=1";echo "\tExec: ${cmd}"; eval ${cmd} &> /dev/null

    func_get_rename_quota_used ${srcquota}; src_old_used=${global_used}

    cmd="mv $src_file $noquota_file"; echo -n "\tExec: ${cmd}"; eval ${cmd} &> ${LOGFILE};
    
    echo ""; echo "  after mv:"
    cmd="ls -l $srcdir | grep -v total"; echo -n "\t${cmd}\t>>"; eval ${cmd}; echo ""
    func_get_rename_quota_used ${srcquota}; src_new_used=${global_used}

    eval echo -n "==${test_num}==\${msg$test_num}===="
    if (($src_old_used == $src_size)) && (($src_new_used == 0)) 
        then echo "success\n"; else echo "fail\n"; exit 1; fi
}

function rename_from_noquota_to_destquota() {
    noquota_size=$(($1 * 1024 * 1024))
    dest_size=$(($2 * 1024 * 1024))
    src_want=0
    dest_want=$src_size

    clean_all_dir;
    cmd="dd if=/dev/zero of=$noquota_file bs=$noquota_size count=1";echo "\tExec: ${cmd}"; eval ${cmd} &> /dev/null
    cmd="dd if=/dev/zero of=$dest_file bs=$dest_size count=1";echo "\tExec: ${cmd}"; eval ${cmd} &> /dev/null

    func_get_rename_quota_used ${destquota}; dest_old_used=${global_used}

    cmd="mv $noquota_file $dest_file"; echo "\tExec: ${cmd}"; eval ${cmd} &> ${LOGFILE};
    exceed=`cat ${LOGFILE} | grep "Disk quota exceeded" | wc -l`
    if (( ${exceed} == 1 )); then echo "\tDisk quota exceeded, 测试成功"; fi
    
    echo ""; echo "  after mv:"
    cmd="ls -l $destdir | grep -v total"; echo -n "\t${cmd}\t>>"; eval ${cmd}
    func_get_rename_quota_used ${destquota}; dest_new_used=${global_used}

    eval echo -n "==${test_num}==\${msg$test_num}===="
    if (( ${exceed} != 1 )); then
        if (($dest_old_used == $dest_size)) && (($dest_new_used == $noquota_size))
            then echo "success\n"; else echo "fail\n"; exit 1; fi
    else
        if (($dest_old_used == $dest_size)) && (($dest_new_used == $dest_size))
            then echo "success\n"; else echo "fail\n"; exit 1; fi
    fi
}

echo "/********************************************************************"
echo "测试 rename/mv 文件====源文件和目标文件在不同配额目录, 且目标文件存在"
echo "********************************************************************/"
if [ "$1" == "--help" ] || [ -z "$1" ];then print_help; fi
source ${QUOTA_COMMON_FUNC}

reset_all 10 5;

test_num=1; if (($1 == ${test_num})) || (($1 == 0));then rename_from_srcquota_to_destquota 1 2; fi
test_num=2; if (($1 == ${test_num})) || (($1 == 0));then rename_from_srcquota_to_destquota 6 2; fi
test_num=3; if (($1 == ${test_num})) || (($1 == 0));then rename_from_srcquota_to_noquota 6 2; fi
test_num=4; if (($1 == ${test_num})) || (($1 == 0));then rename_from_noquota_to_destquota 1 2; fi
test_num=5; if (($1 == ${test_num})) || (($1 == 0));then rename_from_noquota_to_destquota 6 2; fi

echo "/******************************************************************************"
echo "测试 rename/mv 文件====源文件和目标文件在不同配额目录, 且目标文件存在 all success"
echo "******************************************************************************/"
exit 0;



